home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DGPL.ZIP / 3DGPL / CODE / CLIPPER / CLIPP-2D.C next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  8.4 KB  |  218 lines

  1. /** 3DGPL **************************************************\
  2.  *  ()                                                     *
  3.  *  2D line and polygon clipping.                          *
  4.  *                                                         *
  5.  *  Defines:                                               *
  6.  *   C_line_x_clipping       Clipping a line horizontally; *
  7.  *   C_line_y_clipping       Clipping a line vertically;   *
  8.  *                                                         *
  9.  *   C_polygon_x_clipping    Horizontal polygon cliping.   *
  10.  *                                                         *
  11.  *  (6/1995) By Sergei Savchenko. (savs@cs.mcgill.ca).     *
  12.  *  Copyright (c) 1995 Sergei Savchenko.                   *
  13.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES  *
  14.  *  WITHOUT AUTHORISATION                                  *
  15. \***********************************************************/
  16.  
  17. #include "../hardware/hardware.h"           /* hardware specific stuff */
  18. #include "../clipper/clipper.h"             /* 2D macros */
  19.  
  20. int C_2D_clipping;                          /* type of performed clipping */
  21.  
  22. /**********************************************************\
  23.  *  Line clipping using binary search technique.          *
  24.  *                                                        *
  25.  *  RETUNRNS: 0, when line is compleately ouside          *
  26.  *  --------- 1, boundaries otherwise.                    *
  27.  *                                                        *
  28.  *  SETS:  C_2D_clipping to 1 when both first or both     *
  29.  *  -----  points were changed, 0 otherwise.              *
  30. \**********************************************************/
  31.  
  32. int C_line_x_clipping(int **vertex1,int **vertex2,int dimension)
  33. {
  34.  register int i;
  35.  register int whereto;
  36.  register int *l,*r,*m,*t;                  /* left right and midle and tmp */
  37.  static int g_store0[C_MAX_DIMENSIONS];     /* static stores for clipped vxes */
  38.  static int g_store1[C_MAX_DIMENSIONS];
  39.  static int g_store2[C_MAX_DIMENSIONS];
  40.  static int g_store3[C_MAX_DIMENSIONS];
  41.  static int g_store4[C_MAX_DIMENSIONS];
  42.  static int g_store5[C_MAX_DIMENSIONS];
  43.  int **vmn,**vmx;                           /* so that *vmn[0] < *vmx[0] */
  44.  int swap;                                  /* were coordinates swaped? */
  45.  
  46.  C_2D_clipping=0;                           /* default no clipping yet */
  47.  
  48.  if((*vertex1)[0]<(*vertex2)[0])
  49.  { swap=0; vmn=vertex1; vmx=vertex2; }      /* so that *vmn[0] < *vmx[0] */
  50.  else
  51.  { swap=1; vmn=vertex2; vmx=vertex1; }
  52.  
  53.  if(((*vmn)[0]>C_X_CLIPPING_MAX)||((*vmx)[0]<C_X_CLIPPING_MIN)) return(0);
  54.  else
  55.  {
  56.   if((*vmn)[0]<=C_X_CLIPPING_MIN)           /* clipping */
  57.   {
  58.    HW_copy_int(*vmn,l=g_store0,dimension);  /* copying old vertices */
  59.    HW_copy_int(*vmx,m=g_store1,dimension);
  60.    r=g_store2;                              
  61.  
  62.    whereto=0;
  63.    while(m[0]!=C_X_CLIPPING_MIN)
  64.    {
  65.     if(whereto==1) { t=l; l=m; m=t; }
  66.     else           { t=r; r=m; m=t; }
  67.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  68.     whereto=m[0]<C_X_CLIPPING_MIN;
  69.    }
  70.    *vmn=m;                                  /* that is why m[] is static */
  71.    C_2D_clipping=swap^1;
  72.   }
  73.  
  74.   if((*vmx)[0]>=C_X_CLIPPING_MAX)           /* clipping */
  75.   {
  76.    HW_copy_int(*vmn,l=g_store3,dimension);  /* copying old vertices */
  77.    HW_copy_int(*vmx,m=g_store4,dimension);
  78.    r=g_store5;                              
  79.  
  80.    whereto=0;
  81.    while(m[0]!=C_X_CLIPPING_MAX)
  82.    {
  83.     if(whereto==1) { t=l; l=m; m=t; }
  84.     else           { t=r; r=m; m=t; }
  85.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  86.     whereto=m[0]<C_X_CLIPPING_MAX;
  87.    }
  88.    *vmx=m;                                  /* that is why m[] is static */
  89.    C_2D_clipping|=swap&1;
  90.   }
  91.  }
  92.  return(1);                                 /* partialy or not clipped */
  93. }
  94.  
  95. /**********************************************************\
  96.  *  Line clipping using binary search technique.          *
  97.  *                                                        *
  98.  *  RETUNRNS: 0, when line is compleately ouside          *
  99.  *  --------- 1, boundaries otherwise.                    *
  100.  *                                                        *
  101.  *  SETS:  C_2D_clipping to 1 when both first or both     *
  102.  *  -----  points were changed, 0 otherwise.              *
  103. \**********************************************************/
  104.  
  105. int C_line_y_clipping(int **vertex1,int **vertex2,int dimension)
  106. {
  107.  register int i;
  108.  register int whereto;
  109.  register int *l,*r,*m,*t;                  /* left right and midle and tmp */
  110.  static int g_store0[C_MAX_DIMENSIONS];     /* static stores for clipped vxes */
  111.  static int g_store1[C_MAX_DIMENSIONS];
  112.  static int g_store2[C_MAX_DIMENSIONS];
  113.  static int g_store3[C_MAX_DIMENSIONS];
  114.  static int g_store4[C_MAX_DIMENSIONS];
  115.  static int g_store5[C_MAX_DIMENSIONS];
  116.  int **vmn,**vmx;                           /* so that *vmn[1] < *vmx[1] */
  117.  int swap;                                  /* were coordinates swaped? */
  118.  
  119.  C_2D_clipping=0;                           /* default no clipping yet */
  120.  
  121.  if((*vertex1)[1]<(*vertex2)[1])
  122.  { swap=0; vmn=vertex1; vmx=vertex2; }      /* so that *vmn[1] < *vmx[1] */
  123.  else
  124.  { swap=1; vmn=vertex2; vmx=vertex1; }
  125.  
  126.  if(((*vmn)[1]>C_Y_CLIPPING_MAX)||((*vmx)[1]<C_Y_CLIPPING_MIN)) return(0);
  127.  else
  128.  {
  129.   if((*vmn)[1]<=C_Y_CLIPPING_MIN)           /* clipping */
  130.   {
  131.    HW_copy_int(*vmn,l=g_store0,dimension);  /* copying old vertices */
  132.    HW_copy_int(*vmx,m=g_store1,dimension);
  133.    r=g_store2;                             
  134.  
  135.    whereto=0;
  136.    while(m[1]!=C_Y_CLIPPING_MIN)
  137.    {
  138.     if(whereto==1) { t=l; l=m; m=t; }
  139.     else           { t=r; r=m; m=t; }
  140.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  141.     whereto=m[1]<C_Y_CLIPPING_MIN;
  142.    }
  143.    *vmn=m;                                  /* that is why m[] is static */
  144.    C_2D_clipping=swap^1;
  145.   }
  146.  
  147.   if((*vmx)[1]>=C_Y_CLIPPING_MAX)           /* clipping */
  148.   {
  149.    HW_copy_int(*vmn,l=g_store3,dimension);  /* copying old vertices */
  150.    HW_copy_int(*vmx,m=g_store4,dimension);
  151.    r=g_store5;                             
  152.  
  153.    whereto=0;
  154.    while(m[1]!=C_Y_CLIPPING_MAX)
  155.    {
  156.     if(whereto==1) { t=l; l=m; m=t; }
  157.     else           { t=r; r=m; m=t; }
  158.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  159.     whereto=m[1]<C_Y_CLIPPING_MAX;
  160.    }
  161.    *vmx=m;                                  /* that is why m[] is static */
  162.    C_2D_clipping|=swap&1;
  163.   }
  164.  }
  165.  return(1);                                 /* partialy or not clipped */
  166. }
  167.  
  168. /**********************************************************\
  169.  *  Creating a x-clipped polygon.                         *
  170.  *                                                        *
  171.  *  RETURNS: number of elements in the clipped element.   *
  172.  *  -------  (0 when compleately behind view plane)       *
  173.  *                                                        *
  174.  *          |    |        1-2-3-4-5-6-1  -> 2'-3'-5'-6'   *
  175.  *          |5'  |6'                                      *
  176.  *       5*-*----*-*6      If first dot in the line is    *
  177.  *       /  |    |   \     being clipped both points are  *
  178.  *     4*   |    |    *1   copyed If no clipping or second*
  179.  *       \  |    |   /     dot is clipped then only second*
  180.  *       3*-*----*-*2      dot is copyed. If both points  *
  181.  *          |3'  |2'       are clipped well, neither is   *
  182.  *                         copyed.                        *
  183. \**********************************************************/
  184.  
  185. int C_polygon_x_clipping(register int *from,register int *to,
  186.                          int dimension,int length
  187.                         )
  188. {
  189.  register int i;
  190.  int *v1,*v2,new_lng=0;
  191.  int *first_vrtx=to;                        /* begining of the source */
  192.  
  193.  for(i=0;i<length;i++)                      /* for all edges */
  194.  {
  195.   v1=from; from+=dimension; v2=from;        /* taking two vertices */
  196.  
  197.   if(C_line_x_clipping(&v1,&v2,dimension))  /* clipping */
  198.   {
  199.    if(C_2D_clipping)                        /* depends which one was clipped */
  200.    {
  201.     HW_copy_int(v1,to,dimension); to+=dimension;
  202.     HW_copy_int(v2,to,dimension); to+=dimension;  
  203.     new_lng+=2;                             /* first point clipped */
  204.    }
  205.    else
  206.    {
  207.     HW_copy_int(v2,to,dimension); to+=dimension; 
  208.     new_lng++;                              /* second point clipped */
  209.    }
  210.   }
  211.  }
  212.  HW_copy_int(first_vrtx,to,dimension);      /* looping the polygon vertices */
  213.  
  214.  return(new_lng);
  215. }
  216.  
  217. /**********************************************************/
  218.